package fr.auzaiphe.admin.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import fr.auzaiphe.admin.controller.AdminController;

public class AjoutJoueurFrame extends JFrame
{
	private AdminController controler;
	private SpringLayout layout;

	private JLabel labelNomPersonnage;
	private JTextField textFieldNomPersonnage;
	private JLabel labelPrenomPersonnage;
	private JTextField textFieldPrenomPersonnage;

	private JLabel labelPassword;
	private JTextField textFieldPassword;
	private JLabel labelBackground;
	private JTextArea textAreaBackground;
	private JScrollPane scrollPaneAreaBackground;

	private JLabel labelTemps;
	private JLabel labelHMS;
	private JTextField fieldMinute;
	private JTextField fieldSeconde;
	private JLabel labelDeuxPoints;

	private JButton btnValider;
	private String oldNom;
	private String oldPrenom;

	public AjoutJoueurFrame ( AdminController pController )
	{
		this.controler = pController;
		this.creerFenetreJoueur();
		this.listenerJoueurAdd();
	}

	public AjoutJoueurFrame ( AdminController pController, 
													 String pTextFieldNomPersonnage,
													 String pTextFieldPrenomPersonnage,
													 String pTextFieldPassword,
													 String pTextAreaBackground,
													int pTempsRestant )
	{
		this.controler = pController;
		this.creerFenetreJoueur();
		this.textFieldNomPersonnage.setText(pTextFieldNomPersonnage);
		this.textFieldPrenomPersonnage.setText(pTextFieldPrenomPersonnage);
		this.textFieldPassword.setText(pTextFieldPassword);
		this.textAreaBackground.setText(pTextAreaBackground);
		this.fieldMinute.setText( "" + pTempsRestant / 60 );
		this.fieldSeconde.setText( "" + pTempsRestant % 60 );
		this.listenerJoueurUpdate(pTextFieldNomPersonnage, pTextFieldPrenomPersonnage);
	}
	
	private void creerFenetreJoueur()
	{
		this.setTitle("Ajouter/Modifier Joueur");
		this.setSize(600, 450);
		this.layout = new SpringLayout();
		this.setLayout(this.layout);

		/*
		 * Nom du joueur
		 */
		this.labelNomPersonnage = new JLabel();
		this.labelNomPersonnage.setText("Nom du personnage");
		this.layout.putConstraint( SpringLayout.WEST, this.labelNomPersonnage, 5, SpringLayout.WEST, this );
		this.layout.putConstraint( SpringLayout.NORTH, this.labelNomPersonnage, 5, SpringLayout.NORTH, this );
		this.add(this.labelNomPersonnage);

		this.textFieldNomPersonnage = new JTextField();
		this.layout.putConstraint(SpringLayout.WEST, this.textFieldNomPersonnage, 0, SpringLayout.WEST, this.labelNomPersonnage );
		this.layout.putConstraint(SpringLayout.NORTH, this.textFieldNomPersonnage, 5, SpringLayout.SOUTH, this.labelNomPersonnage);
		this.layout.putConstraint(SpringLayout.EAST, this.textFieldNomPersonnage, 280, SpringLayout.WEST, this );
		this.add(this.textFieldNomPersonnage);

		/*
		 * Prenom du joueur
		 */
		this.labelPrenomPersonnage = new JLabel();
		this.labelPrenomPersonnage.setText("Prénom du personnage");
		this.layout.putConstraint( SpringLayout.NORTH, this.labelPrenomPersonnage, 0, SpringLayout.NORTH, this.labelNomPersonnage);
		this.layout.putConstraint( SpringLayout.WEST, this.labelPrenomPersonnage, 20, SpringLayout.EAST, this.textFieldNomPersonnage);
		this.add( this.labelPrenomPersonnage );

		this.textFieldPrenomPersonnage = new JTextField();
		this.layout.putConstraint(SpringLayout.NORTH, this.textFieldPrenomPersonnage, 5, SpringLayout.SOUTH, this.labelPrenomPersonnage);
		this.layout.putConstraint(SpringLayout.WEST, this.textFieldPrenomPersonnage, 0, SpringLayout.WEST, this.labelPrenomPersonnage);
		this.layout.putConstraint(SpringLayout.EAST, this.textFieldPrenomPersonnage, -5, SpringLayout.EAST, this.getContentPane() );
		this.add(this.textFieldPrenomPersonnage);

		/*
		 * Mot de passe
		 */
		this.labelPassword = new JLabel("Mot de passe");
		this.layout.putConstraint( SpringLayout.NORTH, this.labelPassword, 5, SpringLayout.SOUTH, this.textFieldPrenomPersonnage );
		this.layout.putConstraint(SpringLayout.WEST, this.labelPassword, 5, SpringLayout.WEST, this);
		this.add(this.labelPassword);

		this.textFieldPassword = new JTextField();
		this.layout.putConstraint(SpringLayout.NORTH, this.textFieldPassword, 5, SpringLayout.SOUTH, this.labelPassword );
		this.layout.putConstraint(SpringLayout.WEST, this.textFieldPassword, 5, SpringLayout.WEST, this);
		this.layout.putConstraint(SpringLayout.EAST, this.textFieldPassword, -5, SpringLayout.EAST, this.getContentPane() );
		this.add(this.textFieldPassword);

		/*
		 * Gestion temps de connexion
		 */

		this.labelTemps  = new JLabel( "Temps de connexion (Minutes : Secondes) :" );
		this.layout.putConstraint(SpringLayout.NORTH, this.labelTemps, 10, SpringLayout.SOUTH, this.textFieldPassword );
		this.layout.putConstraint(SpringLayout.WEST, this.labelTemps, 5, SpringLayout.WEST, this.getContentPane() );
		this.add( this.labelTemps );

		this.fieldMinute = new JTextField("10");
		this.layout.putConstraint(SpringLayout.NORTH, this.fieldMinute, 0, SpringLayout.NORTH, this.labelTemps );
		this.layout.putConstraint(SpringLayout.WEST, this.fieldMinute, 15, SpringLayout.EAST, this.labelTemps );
		this.add(this.fieldMinute);

		this.labelDeuxPoints = new JLabel(":");
		this.layout.putConstraint(SpringLayout.NORTH, this.labelDeuxPoints, 0, SpringLayout.NORTH, this.fieldMinute );
		this.layout.putConstraint(SpringLayout.WEST, this.labelDeuxPoints, 5, SpringLayout.EAST, this.fieldMinute );
		this.add( this.labelDeuxPoints );

		this.fieldSeconde = new JTextField("00");
		this.layout.putConstraint(SpringLayout.NORTH, this.fieldSeconde, 0, SpringLayout.NORTH, this.labelTemps );
		this.layout.putConstraint(SpringLayout.WEST, this.fieldSeconde, 5, SpringLayout.EAST, this.labelDeuxPoints );
		this.add(this.fieldSeconde);

		/*
		 * Bouton valider
		 */
		this.btnValider = new JButton("Valider");
		this.layout.putConstraint(SpringLayout.SOUTH, this.btnValider, -5, SpringLayout.SOUTH, this.getContentPane() );
		this.layout.putConstraint(SpringLayout.EAST, this.btnValider, -5, SpringLayout.EAST, this.getContentPane() );
		this.add(this.btnValider);

		/*
		 * Background du personnage
		 */
		this.labelBackground = new JLabel("Background du personnage");
		this.layout.putConstraint(SpringLayout.NORTH, this.labelBackground, 5, SpringLayout.SOUTH, this.fieldSeconde );
		this.layout.putConstraint(SpringLayout.WEST, this.labelBackground, 0, SpringLayout.WEST, this.labelTemps );
		this.add(this.labelBackground);

		this.textAreaBackground = new JTextArea();
		this.scrollPaneAreaBackground = new JScrollPane( this.textAreaBackground );
		this.layout.putConstraint(SpringLayout.NORTH, this.scrollPaneAreaBackground, 5, SpringLayout.SOUTH, this.labelBackground );
		this.layout.putConstraint(SpringLayout.WEST, this.scrollPaneAreaBackground, 0, SpringLayout.WEST, this.textFieldPassword );
		this.layout.putConstraint(SpringLayout.EAST, this.scrollPaneAreaBackground, 0, SpringLayout.EAST, this.textFieldPassword );
		this.layout.putConstraint(SpringLayout.SOUTH, this.scrollPaneAreaBackground, -5, SpringLayout.NORTH, this.btnValider );
		this.add( this.scrollPaneAreaBackground );
	}
	
	private void listenerJoueurAdd(){
		this.btnValider.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				AdminPanel.netoyerInputEntier(AjoutJoueurFrame.this.fieldMinute);
				AdminPanel.netoyerInputEntier(AjoutJoueurFrame.this.fieldSeconde);
				if(textFieldNomPersonnage.getText().equals("") || textFieldPrenomPersonnage.getText().equals(""))
				{
					JOptionPane.showMessageDialog(null, "Les champs nom et prenom doivent être renseignés.", "Erreur", JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					AjoutJoueurFrame.this.controler.addJoueur(
							AjoutJoueurFrame.this.textFieldNomPersonnage.getText(),
							AjoutJoueurFrame.this.textFieldPrenomPersonnage.getText(),
							AjoutJoueurFrame.this.textFieldPassword.getText(),
							AjoutJoueurFrame.this.fieldMinute.getText(),
							AjoutJoueurFrame.this.fieldSeconde.getText(),
							AjoutJoueurFrame.this.textAreaBackground.getText() );
				}
				AjoutJoueurFrame.this.dispose();
			}
		});
	}
	
	private void listenerJoueurUpdate(String pOldNom, String pOldPrenom){
		this.oldNom = pOldNom;
		this.oldPrenom = pOldPrenom;
		this.btnValider.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				AdminPanel.netoyerInputEntier(AjoutJoueurFrame.this.fieldMinute);
				AdminPanel.netoyerInputEntier(AjoutJoueurFrame.this.fieldSeconde);
				AjoutJoueurFrame.this.controler.updateJoueur(
						AjoutJoueurFrame.this.oldNom,
						AjoutJoueurFrame.this.oldPrenom,
						AjoutJoueurFrame.this.textFieldNomPersonnage.getText(),
						AjoutJoueurFrame.this.textFieldPrenomPersonnage.getText(),
						AjoutJoueurFrame.this.textFieldPassword.getText(),
						AjoutJoueurFrame.this.fieldMinute.getText(),
						AjoutJoueurFrame.this.fieldSeconde.getText(),
						AjoutJoueurFrame.this.textAreaBackground.getText() );
				AjoutJoueurFrame.this.dispose();
			}
			
		});
	}
}
